package alo.p6.work2;

/**
 * @ClassName FIndDepthInGenericTree
 * @Description 根据双亲结点数组求树的深度
 * @Author Fanxc
 * @DATE 2021/2/2 11:35
 * @Version 1.0
 */
public class FIndDepthInGenericTree {
     int fIndDepthInGenericTree(int[] P,int n){
         int maxDepth=-1,curDepth,j;
         for (int i = 0; i < n; i++) {
             curDepth=0;
             j=i;
             //不断的回溯，直到根结点
             while (P[j]!=-1){
                 curDepth++;
                 j=P[j];
             }
             if (curDepth>maxDepth){
                 maxDepth=curDepth;
             }
         }
         return maxDepth;
     }
}
